package com.ourteam.system.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.ourteam.system.dao.IBaseSysRoleActionRelateDAO;
import com.ourteam.system.dao.ISysRoleDAO;
import com.ourteam.system.dao.ISysRoleGroupDAO;
import com.ourteam.system.dao.SysActionDAOQueryBean;
import com.ourteam.system.dao.SysRole;
import com.ourteam.system.dao.SysRoleActionRelate;
import com.ourteam.system.dao.SysRoleActionRelateDAOQueryBean;
import com.ourteam.system.dao.SysRoleDAOQueryBean;
import com.ourteam.system.dao.SysRoleGroup;
import com.ourteam.system.dao.SysRoleGroupDAOQueryBean;
import com.ourteam.system.dao.SysRoleModuleRelate;
import com.ourteam.system.dao.SysRoleModuleRelateDAOQueryBean;
import com.ourteam.system.dao.SysRoleRelate;
import com.ourteam.system.dao.SysRoleRelateDAOQueryBean;
import com.ourteam.system.dao.SysRoleResourceRelate;
import com.ourteam.system.dao.SysRoleResourceRelateDAOQueryBean;
import com.ourteam.system.dao.SysUser;
import com.ourteam.system.dao.SysUserRoleRelate;
import com.ourteam.system.dao.SysUserRoleRelateDAOQueryBean;
import com.ourteam.system.domain.SystemAction;
import com.ourteam.system.domain.SystemEventLogBean;
import com.ourteam.system.domain.SystemModule;
import com.ourteam.system.domain.SystemRole;
import com.ourteam.system.domain.SystemRoleGroup;
import com.ourteam.system.domain.SystemRoleGroupRelate;
import com.ourteam.system.domain.SystemRoleResourceRelate;
import com.ourteam.system.domain.SystemUserRoleRelate;

/**
 * ϵͳ��ɫ���� impl Generated Date 2009-07-20 22:45:00
 * 
 * @author $Auto Gen
 */
public class SysRoleServiceImpl extends AbstractSysRoleService {

	private static final Map<Long, List<String>> ROLE_ACTION_MAP = new HashMap<Long, List<String>>();

	@Override
	protected SystemRole[] doGetSysRoles(
			SysRoleDAOQueryBean aSysRoleDAOQueryBean) throws Exception {

		if (aSysRoleDAOQueryBean == null) {
			aSysRoleDAOQueryBean = new SysRoleDAOQueryBean();
		}

		SysRole[] sysRoles = sysRoleDao.query(aSysRoleDAOQueryBean);

		return SystemRole.toArray(sysRoles);
	}

	@Override
	protected int doGetSysRoleCount(SysRoleDAOQueryBean aSysRoleDAOQueryBean)
			throws Exception {

		return sysRoleDao.queryCount(aSysRoleDAOQueryBean);
	}

	@Override
	protected void doCreateSysRole(SystemRole sysRole) throws Exception {
		
		String sysRoleCode = this.systemSerialCodeService.getSerialCode("sysRole");
		
		if(StringUtils.isNotBlank(sysRoleCode)) {
			sysRole.setCode(sysRoleCode);
		}

		SysRole role = sysRole.convertToSysRole();

		if (sysRoleDao.checkIsUnique(role) == false) {
			throw new Exception("Role Code [" + role.getRoleCode()
					+ "] Is In Use");
		}

		sysRoleDao.insert(role);
		
		SystemEventLogBean systemEventLog = new SystemEventLogBean();

		systemEventLog.setEventOperatorId(role.getOperatorId());
		systemEventLog.setEventData(JSON.toJSONString(role, true));
		systemEventLog.setEventSource("sysRole");
		Map<String, String> eventContent = new HashMap<String, String>();

		eventContent.put("dataType", "系统角色");
		eventContent.put("dataDesc", role.getRoleName());

		this.systemEventLogService.addSystemEventLog("ADD_NEW_DATA", systemEventLog, eventContent);

	}

	@Override
	protected void doModifySysRole(SystemRole sysRole) throws Exception {

		SysRole role = sysRole.convertToSysRole();

		if (sysRoleDao.checkIsUnique(role) == false) {
			throw new Exception("Role Code [" + role.getRoleCode()
					+ "] Is In Use");
		}
		
		SysRole oldRole = sysRoleDao.findById(sysRole.getId());

		sysRoleDao.update(role);
		
		SystemEventLogBean systemEventLog = new SystemEventLogBean();

		systemEventLog.setEventOperatorId(role.getOperatorId());
		systemEventLog.setEventData(JSON.toJSONString(role, true));
		systemEventLog.setEventSource("sysRole");
		Map<String, String> eventContent = new HashMap<String, String>();

		eventContent.put("dataType", "系统角色");
		eventContent.put("dataDesc", role.getRoleName());
		
		this.systemEventLogService.addSystemEventLog("MODIFY_DATA", systemEventLog, eventContent);
		
		if(oldRole.getRoleName().equals(role.getRoleName()) == false) {
			SysUserRoleRelateDAOQueryBean queryBean = new SysUserRoleRelateDAOQueryBean();
			
			queryBean.createCriteria().andRoleIdEqualTo(sysRole.getId());
			
			SysUserRoleRelate[] userRoleRelate = this.sysUserRoleRelateDao.query(queryBean);
			
			for (int i = 0; i < userRoleRelate.length; i++) {
				SysUserRoleRelate sysUserRoleRelate = userRoleRelate[i];
				SysUser sysUser = this.sysUserDao.findById(sysUserRoleRelate.getUserId());
				if(sysUser.getUserRole() != null) {
					sysUser.setUserRole(StringUtils.replace(sysUser.getUserRole(),oldRole.getRoleName(),role.getRoleName()));
				}
				else {
					sysUser.setUserRole(role.getRoleName());
				}
				
				sysUserDao.update(sysUser);
			}
		}
	}

	@Override
	protected SystemRoleResourceRelate[] doGetSysRoleResourceRelates(
			long aSysRoleId) throws Exception {

		SysRoleDAOQueryBean queryBean = new SysRoleDAOQueryBean();

		queryBean.createCriteria().andIdEqualTo(Long.valueOf(aSysRoleId));

		SysRoleResourceRelate[] sysRoleResourceRelates = sysRoleDao
				.querySysRoleResourceRelate(queryBean);

		SystemRoleResourceRelate[] relates = new SystemRoleResourceRelate[sysRoleResourceRelates.length];

		for (int i = 0; i < relates.length; i++) {
			relates[i] = new SystemRoleResourceRelate(sysRoleResourceRelates[i]);

		}

		return relates;
	}

	@Override
	protected void doSetSysRoleResourceRelates(long aSysRoleId,
			SystemRoleResourceRelate[] aSystemRoleResourceRelates)
			throws Exception {

		SysRoleResourceRelateDAOQueryBean queryBean = new SysRoleResourceRelateDAOQueryBean();

		queryBean.createCriteria()
				.andSysRoleIdEqualTo(Long.valueOf(aSysRoleId));

		SysRoleResourceRelate[] sysRoleResourceRelates = new SysRoleResourceRelate[aSystemRoleResourceRelates.length];

		for (int i = 0; i < sysRoleResourceRelates.length; i++) {
			sysRoleResourceRelates[i] = aSystemRoleResourceRelates[i]
					.convertToSysRoleResourceRelate();

			sysRoleResourceRelates[i].setSysRoleId(aSysRoleId);

		}

		sysRoleResourceRelateDao.synchronize(sysRoleResourceRelates, queryBean);
	}

	@Override
	protected void doCreateSysRoleGroup(SystemRoleGroup aSystemRoleGroup)
			throws Exception {
		
		String sysRoleCode = this.systemSerialCodeService.getSerialCode("sysRoleGroup");
		
		if(StringUtils.isNotBlank(sysRoleCode)) {
			aSystemRoleGroup.setCode(sysRoleCode);
		}

		SysRoleGroup roleGroup = aSystemRoleGroup.convertToSysRoleGroup();

		if (sysRoleGroupDao.checkIsUnique(roleGroup) == false) {
			throw new Exception("Role Group Code [" + roleGroup.getRoleCode()
					+ "] Is In Use");
		}

		sysRoleGroupDao.insert(roleGroup);
		
		SystemEventLogBean systemEventLog = new SystemEventLogBean();

		systemEventLog.setEventOperatorId(roleGroup.getOperatorId());
		systemEventLog.setEventData(JSON.toJSONString(roleGroup, true));
		systemEventLog.setEventSource("sysRole");
		Map<String, String> eventContent = new HashMap<String, String>();

		eventContent.put("dataType", "系统角色组");
		eventContent.put("dataDesc", roleGroup.getRoleName());

		this.systemEventLogService.addSystemEventLog("ADD_NEW_DATA", systemEventLog, eventContent);

	}

	@Override
	protected int doGetSysRoleGroupCount(
			SysRoleGroupDAOQueryBean aSysRoleGroupDAOQueryBean)
			throws Exception {

		return sysRoleGroupDao.queryCount(aSysRoleGroupDAOQueryBean);
	}

	@Override
	protected SystemRoleGroup[] doGetSysRoleGroups(
			SysRoleGroupDAOQueryBean aSysRoleGroupDAOQueryBean)
			throws Exception {

		SysRoleGroup[] roleGroups = sysRoleGroupDao
				.query(aSysRoleGroupDAOQueryBean);

		SystemRoleGroup[] systemRoleGroups = new SystemRoleGroup[roleGroups.length];

		for (int i = 0; i < systemRoleGroups.length; i++) {
			systemRoleGroups[i] = new SystemRoleGroup(roleGroups[i]);

		}

		return systemRoleGroups;
	}

	@Override
	protected void doModifySysRoleGroup(SystemRoleGroup aSystemRoleGroup)
			throws Exception {
		SysRoleGroup roleGroup = aSystemRoleGroup.convertToSysRoleGroup();

		if (sysRoleGroupDao.checkIsUnique(roleGroup) == false) {
			throw new Exception("Role Group Code [" + roleGroup.getRoleCode()
					+ "] Is In Use");
		}

		sysRoleGroupDao.update(roleGroup);
		
		SystemEventLogBean systemEventLog = new SystemEventLogBean();

		systemEventLog.setEventOperatorId(roleGroup.getOperatorId());
		systemEventLog.setEventData(JSON.toJSONString(roleGroup, true));
		systemEventLog.setEventSource("roleGroup");
		Map<String, String> eventContent = new HashMap<String, String>();

		eventContent.put("dataType", "系统角色组");
		eventContent.put("dataDesc", roleGroup.getRoleName());
		
		this.systemEventLogService.addSystemEventLog("MODIFY_DATA", systemEventLog, eventContent);

	}

	@Override
	protected SystemRoleGroupRelate[] doGetSysRoleGroupRelatedRoles(
			long aSysRoleGroupId) throws Exception {

		SysRoleGroupDAOQueryBean queryBean = new SysRoleGroupDAOQueryBean();

		queryBean.createCriteria().andIdEqualTo(Long.valueOf(aSysRoleGroupId));

		SysRoleRelate[] roleRelates = sysRoleGroupDao
				.querySysRoleRelate(queryBean);

		SystemRoleGroupRelate[] systemRoleGroupRelates = new SystemRoleGroupRelate[roleRelates.length];

		for (int i = 0; i < systemRoleGroupRelates.length; i++) {
			systemRoleGroupRelates[i] = new SystemRoleGroupRelate(
					roleRelates[i]);
		}

		return systemRoleGroupRelates;
	}

	@Override
	protected void doSetSysRoleGroupRelatedRoles(long aSysRoleGroupId,
			SystemRoleGroupRelate[] aSystemRoleGroupRelates) throws Exception {

		SysRoleRelate[] relates = new SysRoleRelate[aSystemRoleGroupRelates.length];

		for (int i = 0; i < relates.length; i++) {
			aSystemRoleGroupRelates[i].setSysRoleGroupId(aSysRoleGroupId);
			relates[i] = aSystemRoleGroupRelates[i].convertToSysRoleRelate();
		}

		SysRoleRelateDAOQueryBean queryBean = new SysRoleRelateDAOQueryBean();

		queryBean.createCriteria().andSysRoleGroupIdEqualTo(
				Long.valueOf(aSysRoleGroupId));

		sysRoleRelateDao.synchronize(relates, queryBean);
	}

	@Override
	protected SystemRoleResourceRelate[] doGetSysRoleResourceRelatesByRoleCode(
			String[] aSysRoleCodes) throws Exception {

		SysRoleDAOQueryBean queryBean = new SysRoleDAOQueryBean();

		queryBean.createCriteria().andRoleCodeIn(Arrays.asList(aSysRoleCodes));

		SysRoleResourceRelate[] sysRoleResourceRelates = sysRoleDao
				.querySysRoleResourceRelate(queryBean);

		SystemRoleResourceRelate[] relates = new SystemRoleResourceRelate[sysRoleResourceRelates.length];

		for (int i = 0; i < relates.length; i++) {
			relates[i] = new SystemRoleResourceRelate(sysRoleResourceRelates[i]);

		}

		return relates;

	}

	@Override
	protected SystemRole doGetSysRoleById(long sysRoleId) throws Exception {
		SysRole sysRole = sysRoleDao.findById(sysRoleId);
		return sysRole == null ? null : new SystemRole(sysRole);
	}

	@Override
	protected SystemRoleGroup doGetSysRoleGroupById(long systemRoleGroupId)
			throws Exception {
		SysRoleGroup sysRoleGroup = sysRoleGroupDao.findById(systemRoleGroupId);
		return sysRoleGroup == null ? null : new SystemRoleGroup(sysRoleGroup);
	}

	@Override
	protected void doDeleteSysRole(long sysRoleId,long operatorId) throws Exception {
		SysRoleRelateDAOQueryBean queryBean = new SysRoleRelateDAOQueryBean();

		queryBean.createCriteria().andRelatedSysRoleIdEqualTo(sysRoleId);

		this.sysRoleRelateDao.delete(queryBean);
		
		SysRole sysRole = sysRoleDao.findById(sysRoleId);
		
		SystemEventLogBean systemEventLog = new SystemEventLogBean();

		systemEventLog.setEventOperatorId(operatorId);

		systemEventLog.setEventSource("sysRole");
		Map<String, String> eventContent = new HashMap<String, String>();

		eventContent.put("dataType", "系统角色");
		eventContent.put("dataDesc", sysRole.getRoleName());

		this.systemEventLogService.addSystemEventLog("DELETE_DATA", systemEventLog, eventContent);

		sysRoleDao.delete(sysRoleId);
	}

	@Override
	protected void doDeleteSysRoleGroup(long sysRoleGroupId,long operatorId) throws Exception {
		SysRoleRelateDAOQueryBean queryBean = new SysRoleRelateDAOQueryBean();

		queryBean.createCriteria().andSysRoleGroupIdEqualTo(sysRoleGroupId);

		sysRoleRelateDao.delete(queryBean);
		
		SysRoleGroup sysRoleGroup = sysRoleGroupDao.findById(sysRoleGroupId);
		
		SystemEventLogBean systemEventLog = new SystemEventLogBean();

		systemEventLog.setEventOperatorId(operatorId);

		systemEventLog.setEventSource("sysRoleGroup");
		Map<String, String> eventContent = new HashMap<String, String>();

		eventContent.put("dataType", "系统角色组");
		eventContent.put("dataDesc", sysRoleGroup.getRoleName());

		this.systemEventLogService.addSystemEventLog("DELETE_DATA", systemEventLog, eventContent);

		sysRoleGroupDao.delete(sysRoleGroupId);

	}

	@Override
	protected SystemRole[] doQuerySysRoles(SysRoleDAOQueryBean queryBean)
			throws Exception {
		SysRole[] sysRoles = sysRoleDao.query(queryBean);
		return SystemRole.toArray(sysRoles);
	}

	@Override
	protected SystemAction[] doGetSysRoleActions(long[] sysRoleIds)
			throws Exception {

		if (ArrayUtils.isEmpty(sysRoleIds)) {
			return new SystemAction[0];
		}

		SysActionDAOQueryBean queryBean = new SysActionDAOQueryBean();

		SysRoleActionRelateDAOQueryBean jionCriterion = new SysRoleActionRelateDAOQueryBean();

		jionCriterion.createCriteria().andRoleIdIn(sysRoleIds);

		queryBean.addJoin(IBaseSysRoleActionRelateDAO.EntityName, jionCriterion);

		queryBean.createCriteria().andIdEqualToForeignKey(
				IBaseSysRoleActionRelateDAO.ActionId);

		SystemAction[] actions = SystemAction.toArray(sysActionDao
				.query(queryBean));

		List<SystemAction> systemActions = new ArrayList<SystemAction>();

		for (int i = 0; i < actions.length; i++) {
			SystemAction sysAction = actions[i];
			if (systemActions.contains(sysAction) == false) {
				systemActions.add(sysAction);
			}
		}

		return systemActions.toArray(new SystemAction[systemActions.size()]);
	}

	@Override
	protected void doSetSysRoleActions(long sysRoleId, long[] actionIds)
			throws Exception {

		SysRoleActionRelateDAOQueryBean queryBean = new SysRoleActionRelateDAOQueryBean();

		queryBean.createCriteria().andRoleIdEqualTo(sysRoleId);

		sysRoleActionRelateDao.delete(queryBean);

		SysRoleActionRelate[] roleActionRelates = new SysRoleActionRelate[actionIds.length];

		for (int i = 0; i < roleActionRelates.length; i++) {
			roleActionRelates[i] = new SysRoleActionRelate();

			roleActionRelates[i].setRoleId(sysRoleId);

			roleActionRelates[i].setActionId(actionIds[i]);

		}

		sysRoleActionRelateDao.batchInsert(roleActionRelates);

		ROLE_ACTION_MAP.remove(Long.valueOf(sysRoleId));

	}

	@Override
	protected boolean doIsSysRoleHasAction(long roleId, String domainCode,
			String moduleCode, String actionCode) throws Exception {

		Long sysRoleId = Long.valueOf(roleId);

		SysActionDAOQueryBean queryBean = new SysActionDAOQueryBean();

		SysActionDAOQueryBean.Criteria criteria = queryBean.createCriteria();

		criteria.andActionCodeEqualTo(actionCode);
		if (StringUtils.isNotBlank(moduleCode)) {
			criteria.andModuleCodeEqualTo(moduleCode);
		}

		criteria.andDomainCodeEqualTo(domainCode);

		if (this.sysActionDao.queryCount(queryBean) == 0) {
			return true;
		}

		if (ROLE_ACTION_MAP.containsKey(sysRoleId) == false) {
			SystemAction[] actions = getSysRoleActions(new long[] { roleId });

			List<String> actionIds = new ArrayList<String>();

			for (int i = 0; i < actions.length; i++) {
				SystemAction systemAction = actions[i];
				actionIds.add(systemAction.getDomainCode() + "."
						+ systemAction.getModuleCode() + "."
						+ systemAction.getActionCode());
			}

			ROLE_ACTION_MAP.put(sysRoleId, actionIds);
		}

		List<String> actionIds = ROLE_ACTION_MAP.get(sysRoleId);

		return actionIds.contains(domainCode + "." + moduleCode + "."
				+ actionCode);
	}

	@Override
	protected SystemModule[] doGetSysRoleModules(long[] sysRoleIds)
			throws Exception {

		if (ArrayUtils.isEmpty(sysRoleIds)) {
			return new SystemModule[0];
		}

		SysRoleModuleRelateDAOQueryBean queryBean = new SysRoleModuleRelateDAOQueryBean();

		queryBean.createCriteria().andRoleIdIn(sysRoleIds);

		return SystemModule.toArray(this.sysRoleModuleRelateDao
				.querySysModule(queryBean));
	}

	@Override
	protected void doSetSysRoleModules(long sysRoleId, long[] sysModuleIds)
			throws Exception {

		SysRoleModuleRelateDAOQueryBean queryBean = new SysRoleModuleRelateDAOQueryBean();

		queryBean.createCriteria().andRoleIdEqualTo(sysRoleId);

		sysRoleModuleRelateDao.delete(queryBean);

		SysRoleModuleRelate[] roleModuleRelates = new SysRoleModuleRelate[sysModuleIds.length];

		for (int i = 0; i < roleModuleRelates.length; i++) {
			roleModuleRelates[i] = new SysRoleModuleRelate();

			roleModuleRelates[i].setRoleId(sysRoleId);

			roleModuleRelates[i].setModuleId(sysModuleIds[i]);

		}

		sysRoleModuleRelateDao.batchInsert(roleModuleRelates);
	}

	@Override
	protected SystemRole[] doGetSysRolesByModuleCodes(String domainCode,
			String[] moduleCodes) throws Exception {

		SysRoleModuleRelateDAOQueryBean queryBean = new SysRoleModuleRelateDAOQueryBean();

		queryBean.setDistinct(true);

		SysRoleModuleRelateDAOQueryBean.Criteria criteria = queryBean
				.createCriteria();

		criteria.andDomainCodeEqualTo(domainCode);

		if (ArrayUtils.isNotEmpty(moduleCodes)) {
			criteria.andModuleCodeIn(moduleCodes);
		}

		SysRoleModuleRelate[] roleModuleRelates = this.sysRoleModuleRelateDao
				.query(queryBean);

		if (ArrayUtils.isNotEmpty(roleModuleRelates)) {

			long[] roleIds = new long[roleModuleRelates.length];

			for (int i = 0; i < roleIds.length; i++) {
				roleIds[i] = roleModuleRelates[i].getRoleId();

			}

			SysRoleDAOQueryBean roleDAOQueryBean = new SysRoleDAOQueryBean();

			roleDAOQueryBean.createCriteria().andIdIn(roleIds);

			return SystemRole.toArray(this.sysRoleDao.query(roleDAOQueryBean));
		} else {
			return new SystemRole[0];
		}

	}

	@Override
	protected SystemRole[] doGetSysUserRoles(long sysUserId) throws Exception {
		SystemUserRoleRelate[] userAuthorities = this.sysUserService
				.getSysUserAuthorities(sysUserId);

		List<Long> roleIds = new ArrayList<Long>();

		for (int i = 0; i < userAuthorities.length; i++) {
			SystemUserRoleRelate systemUserAuthority = userAuthorities[i];
			if ("ROLE_GROUP"
					.equalsIgnoreCase(systemUserAuthority.getRoleType())) {

				SystemRoleGroupRelate[] roleGroupRelates = this
						.getSysRoleGroupRelatedRoles(systemUserAuthority
								.getRoleId());

				for (int j = 0; j < roleGroupRelates.length; j++) {
					SystemRoleGroupRelate systemRoleGroupRelate = roleGroupRelates[j];
					Long id = Long.valueOf(systemRoleGroupRelate
							.getRelatedSysRoleId());
					if (roleIds.contains(id) == false) {
						roleIds.add(id);
					}
				}

			} else {
				Long id = Long.valueOf(systemUserAuthority.getRoleId());
				if (roleIds.contains(id) == false) {
					roleIds.add(id);
				}
			}
		}

		if (roleIds.isEmpty()) {
			return new SystemRole[0];
		} else {
			SysRoleDAOQueryBean queryBean = new SysRoleDAOQueryBean();

			queryBean.setDistinct(true);

			queryBean.createCriteria().andIdIn(roleIds);

			return getSysRoles(queryBean);
		}

	}

	@Override
	protected long[] doGetDefaultSysRoleIds() throws Exception {

		List<Long> list = new ArrayList<Long>();

		SysRoleDAOQueryBean queryBean = new SysRoleDAOQueryBean();

		queryBean.setDistinct(true);

		queryBean.createCriteria().andDefaultFlagEqualTo("true");

		queryBean.addIdSelectProperty();

		Map<String, Object>[] roleMaps = this.sysRoleDao
				.querySelective(queryBean);

		for (int i = 0; i < roleMaps.length; i++) {
			list.add(MapUtils.getLong(roleMaps[i], ISysRoleDAO.DataProperty.Id));

		}

		SysRoleGroupDAOQueryBean roleGroupQueryBean = new SysRoleGroupDAOQueryBean();

		roleGroupQueryBean.setDistinct(true);

		roleGroupQueryBean.createCriteria().andDefaultFlagEqualTo("true");

		roleGroupQueryBean.addIdSelectProperty();

		Map<String, Object>[] roleGroupMaps = this.sysRoleGroupDao
				.querySelective(roleGroupQueryBean);

		for (int i = 0; i < roleGroupMaps.length; i++) {
			list.add(MapUtils.getLong(roleGroupMaps[i],
					ISysRoleGroupDAO.DataProperty.Id));
		}

		return ArrayUtils.toPrimitive(list.toArray(new Long[list.size()]));
	}

	@Override
	protected void doSetSysActionRoles(long actionId, long[] roleIds)
			throws Exception {

		SysRoleActionRelate[] roleActionRelates = new SysRoleActionRelate[roleIds.length];

		for (int i = 0; i < roleActionRelates.length; i++) {

			SysRoleActionRelate sysRoleActionRelate = new SysRoleActionRelate();

			sysRoleActionRelate.setActionId(actionId);

			sysRoleActionRelate.setRoleId(roleIds[i]);

			roleActionRelates[i] = sysRoleActionRelate;

		}

		SysRoleActionRelateDAOQueryBean queryBean = new SysRoleActionRelateDAOQueryBean();

		queryBean.createCriteria().andActionIdEqualTo(actionId);

		sysRoleActionRelateDao.delete(queryBean);

		sysRoleActionRelateDao.batchInsert(roleActionRelates);

	}

	@Override
	protected SystemRole[] doGetSysActionRoles(long sysActionId)
			throws Exception {

		SysRoleDAOQueryBean queryBean = new SysRoleDAOQueryBean();

		SysRoleActionRelateDAOQueryBean subQueryBean = new SysRoleActionRelateDAOQueryBean();

		subQueryBean.addRoleIdSelectProperty();

		subQueryBean.createCriteria().andActionIdEqualTo(sysActionId);

		queryBean.createCriteria().andIdIn(subQueryBean,
				this.sysRoleActionRelateDao);

		return SystemRole.toArray(this.sysRoleDao.query(queryBean));
	}

	@Override
	protected SystemRole[] doGetSysRolesByModuleIds(long[] moduleIds)
			throws Exception {

		SysRoleModuleRelateDAOQueryBean queryBean = new SysRoleModuleRelateDAOQueryBean();

		queryBean.addRoleIdSelectProperty();

		queryBean.setDistinct(true);

		SysRoleModuleRelateDAOQueryBean.Criteria criteria = queryBean
				.createCriteria();

		criteria.andModuleIdIn(moduleIds);

		SysRoleDAOQueryBean roleDAOQueryBean = new SysRoleDAOQueryBean();

		roleDAOQueryBean.createCriteria().andIdIn(queryBean,
				sysRoleModuleRelateDao);

		return SystemRole.toArray(this.sysRoleDao.query(roleDAOQueryBean));

	}

	@Override
	protected void doSetSysModuleRoles(long moduleId, long[] roleIds)
			throws Exception {

		SysRoleModuleRelate[] roleModuleRelates = new SysRoleModuleRelate[roleIds.length];

		for (int i = 0; i < roleModuleRelates.length; i++) {
			SysRoleModuleRelate sysRoleModuleRelate = new SysRoleModuleRelate();

			sysRoleModuleRelate.setModuleId(moduleId);

			sysRoleModuleRelate.setRoleId(roleIds[i]);

			roleModuleRelates[i] = sysRoleModuleRelate;

		}

		SysRoleModuleRelateDAOQueryBean queryBean = new SysRoleModuleRelateDAOQueryBean();

		queryBean.createCriteria().andModuleIdEqualTo(moduleId);

		sysRoleModuleRelateDao.delete(queryBean);

		sysRoleModuleRelateDao.batchInsert(roleModuleRelates);

	}

}
